class Admin::BaseController < ApplicationController
  
  layout   'base'

  before_filter :admin_authorize
  include AuthenticatedSystem
  #
  #    protected
  #
  #  ssl_required :admin_authorize if SSL_ENABLE
  def admin_authorize
    if current_user
      return true
    else
      redirect_to new_admin_session_path
      return false
    end
  end

  def is_administrator
    if current_user.is_admim?
      return true
    else
      redirect_to new_admin_session_path
      return false
    end
  end
  
  private
  def get_layout_auth
    if current_user.roles.map(&:name).include?('admin')
      'super_admin'
    else
      'base'
    end
    
  end
  #
  #    # Expend the querying parameters and delete the entries with out values.
  #    # Notice that nested attributes are not supported as key fields to search.
  #    def analyze_conditions(hash = nil)
  #      if hash
  #        conditions = []
  #        hash.each do |key, value|
  #          if value != nil
  #            #          next if value.blank?
  #            if value.blank?
  #              next
  #            end
  #          end
  #
  #          if value =~ /^[\-\.\d]+$/ && ![:tel, :fax, :postal_code, :mobile, :serial,:name].include?(key.to_sym)
  #            conditions << "#{key} = #{value}"
  #          elsif value.is_a?(String)
  #            conditions << value.split(" ").map{ |txt| "#{key} LIKE '%#{txt}%'" }.join(" AND ")
  #          elsif value.is_a?(Hash)
  #            conditions << "#{key} BETWEEN '1990-1-1' AND '#{value[:to]}' "        if value[:from].blank? and !value[:to].blank?
  #            conditions << "#{key} BETWEEN '#{value[:from]}' AND '9999-12-31'"     if value[:to].blank? and !value[:from].blank?
  #            conditions << "#{key} BETWEEN '#{value[:from]}' AND '#{value[:to]}'"  if !value[:from].blank? and !value[:to].blank?
  #          elsif key == "deleted_at" && value == nil
  #            conditions << "deleted_at IS NULL"
  #          end
  #        end
  #
  #        return conditions.map{ |c| "(#{c})" }.join(" AND ")
  #      else
  #        return []
  #      end
  #    end
  #
  #    # Define the page size which will be used by "will_paginate"
  #    def self.per_page
  #      return 20
  #    end

  def split_keyword(keyword)
    unless keyword.blank?
      keyword.gsub!(/[　\s\t]+$/o, "")
      keyword.gsub!(/^[　\s\t]+/o, "")
      keyword.gsub!(/[　\s\t]+/o, " ")
      keyword.split(/(\s)/)
    end
  end
end

